package com.qingmeng.applyjob.portal.utils;

import com.qingmeng.applyjob.model.User;
import com.qingmeng.applyjob.portal.bo.PortalUserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {
    private static final Logger logger = LoggerFactory.getLogger(SecurityUtils.class);

    /**
     * 获取到当前登录用户
     * @return
     */
    public static User getCurrentUser(){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        //如果是匿名登录
        if(authentication.getPrincipal() instanceof  String){
            logger.info("用户匿名登录，登录名称为" + authentication.getPrincipal());
            return null ;
        }
        PortalUserDetails portalUserDetails = (PortalUserDetails) authentication.getPrincipal();
        return portalUserDetails.getUser();
    }
}
